#include "pm/cost.h"

using namespace pm;

const wxString Cost::TABLE_NAME   = wxT("costs");

Cost::Cost()
: db::Object()
, m_unitCost(0)
, m_quantity(0)
, m_date(wxDateTime::Today())
, m_idTask(db::Object::undefined)
{
}

Cost::Cost(wxSQLite3ResultSet& res)
: db::Object(res)
, m_unitCost(res.GetDouble(wxT("unit_cost")))
, m_quantity(res.GetDouble(wxT("quantity")))
, m_name(    res.GetString(wxT("name")))
, m_date(    res.GetDate(  wxT("date")))
, m_idTask(  res.GetInt(   wxT("id_task")))
{
}

db::update_value_type Cost::prepare_insert(wxSQLite3Database& db)
{
	if (m_idTask == db::Object::undefined) {
		return db::update_value_type(false, wxSQLite3Statement());
	}
	
	wxSQLite3Statement stmt = db.PrepareStatement("insert into costs values(null, ?, ?, ?, ?, ?)");
	stmt.Bind(1, m_unitCost);
	stmt.Bind(2, m_quantity);
	stmt.Bind(3, m_name);
	stmt.BindDate(4, m_date);
	stmt.Bind(5, m_idTask);
	return db::update_value_type(true, stmt);
}

db::update_value_type Cost::prepare_update(wxSQLite3Database& db)
{
	wxSQLite3Statement stmt = db.PrepareStatement("update costs set unit_cost = ?, quantity = ?, name = ?, date = ?, id_task = ? where id = ?");
	stmt.Bind(1, m_unitCost);
	stmt.Bind(2, m_quantity);
	stmt.Bind(3, m_name);
	stmt.BindDate(4, m_date);
	stmt.Bind(5, m_idTask);
	stmt.Bind(6, id());
	return db::update_value_type(true, stmt);
}


const wxString CostInfo::TABLE_NAME = wxT("view_costs");

CostInfo::CostInfo()
: Cost()
{
}

CostInfo::CostInfo(wxSQLite3ResultSet& res)
: Cost(res)
, m_idProject(res.GetInt(wxT("id_project")))
, m_taskName(res.GetString(wxT("task_name")))
{
}



GetCostsByProject::GetCostsByProject(db::ID id_project)
: m_idProject(id_project)
{
}

wxSQLite3Statement GetCostsByProject::prepare(wxSQLite3Database& db)
{
	wxSQLite3Statement stmt = db.PrepareStatement("select id, unit_cost, quantity, name, date, id_task, task_name, id_project from view_costs where id_project = ? order by date asc");
	stmt.Bind(1, m_idProject);
	return stmt;
}
